package com.zxx.pattern.statePattern.state;

/**
 * 投币后的状态
 *
 * @author zxx on 2021/1/14
 */
public class StateMoneyInserted implements State {

    private CandyMachine candyMachine;

    public StateMoneyInserted(CandyMachine candyMachine) {
        this.candyMachine = candyMachine;
    }

    @Override
    public void onMoneyInserted() {
        System.out.println("不能多次投币！");
    }

    @Override
    public void onMoneyReturned() {
        System.out.println("正在退钱......退钱成功！");
        candyMachine.setState(candyMachine.getNoMoneyState());
    }

    @Override
    public void onCrankTurned() {
        System.out.println("转动了摇把......");
        int random = (int) (Math.random() * 10000);
        if (random % 10 == 0 && candyMachine.getCandyCount() > 1) {
            System.out.println("恭喜您获得了两颗糖果！");

            //设置状态为StateWinner（获得两颗糖果的状态）
            candyMachine.setState(candyMachine.getWinnerState());
            //调用StateWinner的onCandyPoped方法
            candyMachine.popCandy();
        } else {
            if (candyMachine.getCandyCount() > 0) {
                //设置状态为StateCandyPopping（正在发放糖果的状态）
                candyMachine.setState(candyMachine.getCandyPoppingState());

                //调用StateCandyPopping的onCandyPoped方法
                candyMachine.popCandy();

            } else {
                //设置状态为StateNoCandy（没有糖果的状态）
                candyMachine.setState(candyMachine.getNoCandyState());
                //调用StateNoCandy的onCandyPoped方法
                candyMachine.popCandy();
            }
        }
    }

    @Override
    public void onCandyPoped() {
        System.out.println("请先转动摇把！");
    }
}
